package com.feng.modules.core.action;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.util.ObjectUtils;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.config.entities.Parameterizable;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class BaseAction implements Action, ServletRequestAware,
		ServletResponseAware, SessionAware, ParameterAware,
		ServletContextAware, Parameterizable {
	Log log=LogFactory.getLog(this.getClass());
	private Map params;
	private ServletContext context;
	private Map parameters;
	private Map session;
	private HttpServletResponse response;
	private HttpServletRequest request;
	private Map parametersToMap = new HashMap();

	public void addParam(String name, String value) {
		getParams().put(name, value);
	}

	public Map getParams() {
		return params;
	}

	public void setParams(Map params) {
		this.params = params;
	}

	public ServletContext getServletContext() {
		return context;
	}

	public void setServletContext(ServletContext servletContext) {
		this.context = servletContext;
	}

	public Map getParameters() {
		return parameters;
	}

	public void setParameters(Map<String, String[]> parameters) {
		this.parameters = parameters;
		try {
			for (Iterator it = parameters.keySet().iterator(); it.hasNext();) {
				String key = (String) it.next();
				Object values = parameters.get(key);
				if (ObjectUtils.isArray(values)) {
					Object[] value = ObjectUtils.toObjectArray(parameters
							.get(key));
					if ((value == null) || (value.length == 0)) {
						continue;
					} else if (value.length == 1) {
						parametersToMap.put(key, value[0]);
					} else {
						parametersToMap.put(key, value);
					}
				} else {
					this.parametersToMap.put(key, parameters.get(key));
				}
			}
		} catch (Exception e) {
			log.debug("BaseActionError"+e);
			e.printStackTrace();
		}
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public HttpServletResponse getServletResponse() {
		return response;
	}

	public void setServletResponse(HttpServletResponse servletResponse) {
		this.response = servletResponse;
	}

	public HttpServletRequest getServletRequest() {
		return request;
	}

	public void setServletRequest(HttpServletRequest servletRequest) {
		this.request = servletRequest;
	}

	public String execute() throws Exception {
		return SUCCESS;
	}

	public Map getParametersToMap() {
		return parametersToMap;
	}
	public void outJson(Object obj) throws Exception {
		PrintWriter out = null;
		try {
			this.getServletResponse().setCharacterEncoding("UTF-8");
			out = this.getServletResponse().getWriter();
			if (obj instanceof String) {
				out.write(obj.toString());
			} else {
				out.write(JSON.toJSONString(obj));
			}
		} catch (Exception e) {
		    	e.printStackTrace();
			throw e;
		} finally {
			if(out!=null){
				out.flush();
				out.close();
			}
		}
	}

}
